// 131.分割回文串
package LeetcodeHot100;

import java.util.ArrayList;
import java.util.List;

public class Solution131 {
    public List<List<String>> result = new ArrayList<>();

    public List<List<String>> partition(String s) {
        BackTrace(s, new ArrayList<String>(), 0);

        return result;
    }

    private void BackTrace(String s, ArrayList<String> list, int cur) {
        if (cur == s.length()) {
            result.add(new ArrayList<>(list));
            return;
        }
        if (cur > s.length()) {
            return;
        }
        for (int i = cur + 1; i <= s.length(); i++) {
            if (judge(s.substring(cur, i)) == true) {
                list.add(s.substring(cur, i));
                BackTrace(s, list, i);
                list.remove(list.size() - 1);
            }
        }
    }

    public boolean judge(String s) {
        StringBuilder sb = new StringBuilder(s);
        StringBuilder reverse = sb.reverse();
        return reverse.toString().equals(s);
    }

}
